package cn.oatn;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class T1006_ASM_DeleteMethod {

    public static void test() throws IOException {
        ClassReader cr = new ClassReader("cn.oatn.MyRunnable");
        ClassWriter cw = new ClassWriter(0);

        // 注意cw作为第二个参数
        cr.accept(new ClassVisitor(Opcodes.ASM9, cw) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
                if (name.equals("a")) {
                    return null;
                }
                return super.visitMethod(access, name, descriptor, signature, exceptions);
            }
        }, 0);

        OutputStream o = new FileOutputStream("XXX.class");
        o.write(cw.toByteArray());
        o.close();

    }
}
